﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel.DataAnnotations;
using static FarmSmartSleep.Organ.API.Application.Commands.SheepFarmCommands.SheepFarmSelectHandler.PageDto;

namespace FarmSmartSleep.Organ.API.Application.Commands.SheepFarmCommands
{
    public class SheepFarmSelectHandler : IRequestHandler<SheepFarmSelectCommand, Result>
    {
        private readonly IBaseRepository<SheepFarm> sheepfarmRep;
        private readonly IBaseRepository<City> cityRep;
        private readonly IBaseRepository<Province> provinceRep;
        private readonly IBaseRepository<County> countyRep;
        private readonly IBaseRepository<Departments> departmentsRep;


        public SheepFarmSelectHandler(IBaseRepository<SheepFarm> sheepfarmRep, IBaseRepository<City> cityRep, IBaseRepository<Province> provinceRep, IBaseRepository<County> countyRep, IBaseRepository<Departments> departmentsRep)
        {
            this.sheepfarmRep = sheepfarmRep;
            this.cityRep = cityRep;
            this.provinceRep = provinceRep;
            this.countyRep = countyRep;
            this.departmentsRep = departmentsRep;
        }



        public async Task<Result> Handle(SheepFarmSelectCommand request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CodeEnum.search };
                        
            var data = new PageDto() { PageCount=0,TotalCount=0 };
            var list = (from a in sheepfarmRep.GetAll()
                        join b in provinceRep.GetAll() on a.ProvinceId equals b.ProvinceId
                        join c in cityRep.GetAll() on a.CityId equals c.CityId
                        join d in countyRep.GetAll() on a.CountyId equals d.CountyId
                        join e in departmentsRep.GetAll() on a.DepartmentId equals e.DepartmentId
                        where a.IsDelete == false
                        select new SheepDto()
                        {
                            FarmId = a.FarmId,
                            SerialId = a.SerialId,
                            SheepFarmNo = a.SheepFarmNo,
                            FarmName = a.FarmName,
                            BuildingDate = a.BuildingDate,
                            FarmAddress = a.FarmAddress,
                            FarmState = a.FarmState,
                            CreateName = a.CreateName,
                            CreateDate = a.CreateDate,
                            UpdateName = a.UpdateName,
                            UpdateDate = a.UpdateDate,
                            HeadID = a.HeadID,
                            ProvinceId = a.ProvinceId,
                            ProvinceName = b.ProvinceName,
                            CityId = a.CityId,
                            CityName = c.CityName,
                            CountyId = a.CountyId,
                            CountyName = d.CountyName,
                            DepartmentId = a.DepartmentId,
                            DepartmentName = e.DepartmentName,
                            SheepFarmNick = a.SheepFarmNick,
                            Scale = a.Scale
                        });
            if (!string.IsNullOrEmpty(request.FarmName))
            {
                list = list.Where(x => x.FarmName.Contains(request.FarmName));
            }
            if (request.HeadID > 0)
            {
                list = list.Where(x => x.HeadID == request.HeadID);
            }


            data.TotalCount = list.Count();
            data.PageCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pageSize);
            data.data = list.OrderBy(x => x.FarmId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

            res.Code = (int)CodeEnum.success;
            res.Data = data;
            return res;
        }


        public class PageDto
        {
            public int PageCount { get; set; }
            public int TotalCount { get; set; }
            public List<SheepDto> data { get; set; }

        }


        public class SheepDto
        {
            /// <summary>
            /// 主键ID
            /// </summary>
            public int FarmId { get; set; }
            /// <summary>
            /// 序号
            /// </summary>
            public int SerialId { get; set; }
            /// <summary>
            /// 羊场编号
            /// </summary>
            public string? SheepFarmNo { get; set; }
            /// <summary>
            /// 羊场全称
            /// </summary>
            public string? FarmName { get; set; }
            /// <summary>
            /// 建账日期
            /// </summary>
            public DateTime? BuildingDate { get; set; }
            /// <summary>
            /// 详细地址
            /// </summary>
            public string? FarmAddress { get; set; }
            /// <summary>
            /// 状态
            /// </summary>
            public int FarmState { get; set; }
            /// <summary>
            /// 创建人
            /// </summary>
            public string? CreateName { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime? CreateDate { get; set; }
            /// <summary>
            /// 修改人
            /// </summary>
            public string? UpdateName { get; set; }
            /// <summary>
            /// 修改时间
            /// </summary>
            public DateTime? UpdateDate { get; set; }
            /// <summary>
            /// 逻辑删除
            /// </summary>
            public bool IsDelete { get; set; }
            /// <summary>
            /// 负责人
            /// </summary>
            public int HeadID { get; set; }
            /// <summary>
            /// 管理部门
            /// </summary>
            public int DepartmentId { get; set; }
            /// <summary>
            /// 省id
            /// </summary>
            public int ProvinceId { get; set; }
            /// <summary>
            /// 市id
            /// </summary>
            public int CityId { get; set; }
            /// <summary>
            /// 县id
            /// </summary>
            public int CountyId { get; set; }
            public string? CityName { get; set; }//市名称
            public string CountyName { get; set; }//县 名称
            public string? ProvinceName { get; set; }//省名称
            /// <summary>
            /// 部门名称
            /// </summary>
            public string DepartmentName { get; set; }
            public string? SheepFarmNick { get; set; }//羊场简称
            public int Scale { get; set; }//羊场规模
        }

    }
}
